
######################
# Makefile for dtOS  #
######################


# Entry point of dtOS
# It must be as same as 'KernelEntryPointPhyAddr' in load.inc!!!
ENTRYPOINT	= 0x30400

# Offset of entry point in kernel file
# It depends on ENTRYPOINT
ENTRYOFFSET	=   0x400


# Programs, flags, etc.
ASM		= nasm
DASM		= ndisasm
CC		= gcc
LD		= ld
ASMBFLAGS	= -I code/boot/include/
ASMKFLAGS	= -I code/include/ -f elf
CFLAGS		= -I code/include/ -c -fno-builtin -fno-stack-protector
LDFLAGS		= -s -Ttext $(ENTRYPOINT)
DASMFLAGS	= -u -o $(ENTRYPOINT) -e $(ENTRYOFFSET)

# This Program
TINIXBOOT	= bin/boot.bin bin/loader.bin
TINIXKERNEL	= bin/kernel.bin
OBJS		= code/kernel/kernel.o code/kernel/syscall.o code/kernel/start.o code/kernel/main.o\
			code/kernel/clock.o code/kernel/i8259.o code/kernel/global.o code/kernel/protect.o\
			code/kernel/proc.o code/kernel/keyboard.o code/kernel/tty.o code/kernel/console.o\
			code/kernel/printf.o code/kernel/vsprintf.o\
			code/lib/klib.o code/lib/klibc.o code/lib/string.o
DASMOUTPUT	= kernel.bin.asm

# All Phony Targets
.PHONY : everything final image clean realclean disasm all buildimg

# Default starting position
everything : $(TINIXBOOT) $(TINIXKERNEL)

all : realclean everything

final : all clean

image : final buildimg

clean :
	rm -f $(OBJS)

realclean :
	rm -f $(OBJS) $(TINIXBOOT) $(TINIXKERNEL)

disasm :
	$(DASM) $(DASMFLAGS) $(TINIXKERNEL) > $(DASMOUTPUT)

# Write "boot.bin" & "loader.bin" into floppy image "dtOS.IMG"
# We assume that "dtOS.IMG" exists in current folder
buildimg :
	mount bin/dtOS.IMG /mnt/floppy -o loop
	cp -f bin/loader.bin /mnt/floppy/
	cp -f bin/kernel.bin /mnt/floppy
	umount  /mnt/floppy

bin/boot.bin : code/boot/boot.asm code/boot/include/load.inc code/boot/include/fat12hdr.inc
	$(ASM) $(ASMBFLAGS) -o $@ $<

bin/loader.bin : code/boot/loader.asm code/boot/include/load.inc code/boot/include/fat12hdr.inc code/boot/include/pm.inc
	$(ASM) $(ASMBFLAGS) -o $@ $<

$(TINIXKERNEL) : $(OBJS)
	$(LD) $(LDFLAGS) -o $(TINIXKERNEL) $(OBJS)

code/kernel/kernel.o : code/kernel/kernel.asm code/include/sconst.inc
	$(ASM) $(ASMKFLAGS) -o $@ $<

code/kernel/syscall.o : code/kernel/syscall.asm code/include/sconst.inc
	$(ASM) $(ASMKFLAGS) -o $@ $<

code/kernel/start.o: code/kernel/start.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h code/include/proto.h \
			code/include/tty.h code/include/console.h code/include/global.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/main.o: code/kernel/main.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h code/include/proto.h \
			code/include/tty.h code/include/console.h code/include/global.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/i8259.o: code/kernel/i8259.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h \
			code/include/tty.h code/include/console.h code/include/global.h code/include/proto.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/global.o: code/kernel/global.c code/include/type.h code/include/const.h code/include/protect.h code/include/proc.h \
			code/include/tty.h code/include/console.h code/include/global.h code/include/proto.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/protect.o: code/kernel/protect.c code/include/type.h code/include/const.h code/include/protect.h code/include/proc.h code/include/proto.h \
			code/include/tty.h code/include/console.h code/include/global.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/clock.o: code/kernel/clock.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h \
			code/include/tty.h code/include/console.h code/include/global.h code/include/proto.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/proc.o: code/kernel/proc.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h code/include/proto.h \
			code/include/tty.h code/include/console.h code/include/global.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/keyboard.o: code/kernel/keyboard.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h \
			code/include/tty.h code/include/console.h code/include/global.h code/include/keyboard.h code/include/keymap.h code/include/proto.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/tty.o: code/kernel/tty.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h \
			code/include/tty.h code/include/console.h code/include/global.h code/include/keyboard.h code/include/proto.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/console.o: code/kernel/console.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h \
			code/include/tty.h code/include/console.h code/include/global.h code/include/keyboard.h code/include/proto.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/vsprintf.o: code/kernel/vsprintf.c code/include/type.h code/include/const.h code/include/string.h
	$(CC) $(CFLAGS) -o $@ $<

code/kernel/printf.o: code/kernel/printf.c code/include/type.h code/include/const.h
	$(CC) $(CFLAGS) -o $@ $<

code/lib/klibc.o: code/lib/klib.c code/include/type.h code/include/const.h code/include/protect.h code/include/string.h code/include/proc.h code/include/proto.h \
			code/include/tty.h code/include/console.h code/include/global.h
	$(CC) $(CFLAGS) -o $@ $<

code/lib/klib.o : code/lib/klib.asm code/include/sconst.inc
	$(ASM) $(ASMKFLAGS) -o $@ $<

code/lib/string.o : code/lib/string.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
